package com.quanshi.shopping_user_customer_api.controller;

import cn.hutool.core.util.RandomUtil;
import com.quanshi.shopping_common.entity.ShoppingUser;
import com.quanshi.shopping_common.result.BaseResult;
import com.quanshi.shopping_common.service.IMessageService;
import com.quanshi.shopping_common.service.IShoppingUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 */
@RestController
@RequestMapping("/user/shoppingUser")
public class UserController {



    @DubboReference
    private IMessageService messageService;


    @DubboReference
    private IShoppingUserService shoppingUserService;

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendMessage")
    public BaseResult sendMessage(String phone){

        //生成随机码
        String random = RandomUtil.randomNumbers(4);
        //发送短信
        BaseResult result = messageService.sendMessage(phone, random);
        if(200 == result.getCode()){
            //将随机码存储到redis  设置 1分钟过期
            shoppingUserService.saveRegisterCheckCode(phone,random);

            return BaseResult.success();
        }

        return result;

    }

    /**
     * 检查注册验证码是否正常
     * @param phone 手机号
     * @param checkCode 验证码
     * @return
     */
    @GetMapping("/registerCheckCode")
    public BaseResult registerCheckCode(String phone,String checkCode){

        shoppingUserService.registerCheckCode(phone,checkCode);

        return BaseResult.success();
    }

    /**
     * 注册用户
     * @param shoppingUser 用户对象
     * @return
     */
    @PostMapping("/register")
    public BaseResult register(@RequestBody ShoppingUser shoppingUser){

        shoppingUserService.register(shoppingUser);

        return BaseResult.success();
    }

    /**
     * 登录发送验证码
     * @param phone 手机号
     * @return
     */
    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginCheckCode(String phone){
        //生成随机码
        String random = RandomUtil.randomNumbers(4);

        //发送短信
        BaseResult result = messageService.sendMessage(phone, random);

        //发送短信成
        if(200 == result.getCode()){

            //redis存储验证码
            shoppingUserService.saveLoginCheckCode(phone,random);

            return BaseResult.success();

        }

        return BaseResult.error(result.getMessage());

    }


    /**
     * 使用验证码登录
     * @param phone 手机号
     * @param checkCode 验证码
     * @return
     */
    @PostMapping("/loginCheckCode")
    public BaseResult loginCheckCode(String phone,String checkCode){

       String token =  shoppingUserService.loginCheckCode(phone,checkCode);

        return BaseResult.success(token);
    }

    @PostMapping("/loginPassword")
    public BaseResult loginPassword(@RequestBody ShoppingUser shoppingUser){

        String token = shoppingUserService.loginPassword(shoppingUser.getUsername(), shoppingUser.getPassword());

        return BaseResult.success(token);

    }

    @GetMapping("/getName")
    public BaseResult getUsername(@RequestHeader("Authorization") String authorization){

        String token =  authorization.replaceAll("Bearer ","");

        String username = shoppingUserService.getName(token);
        return BaseResult.success(username);
    }

}
